package com.gxa.modules.service.impl;


import com.gxa.common.constant.Constants;
import com.gxa.common.exception.LcException;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.RoleMapper;
import com.gxa.modules.pojo.Role;
import com.gxa.modules.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired(required = false)
    RoleMapper roleMapper;


    @Override
    public ResultVo getRolePage(PageRecv pageRecv) {
        ResultVo r=new ResultVo();
        List<Role> roleList=roleMapper.queryRoleByPage(pageRecv);
        r.setCode(200);
        r.setData(roleList);
        return r;
    }

    @Override
    public ResultVo addRole(Role role) {
        ResultVo r=new ResultVo();
        role.setR_create_date(new Date());
        role.setR_status(Constants.ROLE_NORMAL_STATUS);
        int insertResult=roleMapper.insertRole(role);
        if(insertResult<=0){
            throw new LcException(500,"添加角色失败！");
        }
        r.setCode(200);
        r.setMsg("添加角色成功！");
        return r;
    }

    @Override
    public ResultVo modifyRole(Role role) {
        ResultVo r=new ResultVo();
        int updateResult=roleMapper.updateRole(role);
        if(updateResult<=0){
            throw new LcException(500,"修改角色失败！");
        }
        r.setCode(200);
        r.setMsg("修改角色成功！");
        return r;
    }

    @Override
    public ResultVo delRole(Role role) {
        ResultVo r=new ResultVo();
        role.setR_status(Constants.ROLE_DEL_STATUS);
        int updateResult=roleMapper.updateRole(role);
        if(updateResult<=0){
            throw new LcException(500,"删除角色失败！");
        }
        r.setCode(200);
        r.setMsg("删除角色成功！");
        return r;
    }
}
